﻿using System.ComponentModel;
using Dqs_BLL;
using Dqs_DAL;
using Model;
using Sunny.UI;

namespace Dqs_UI
{
    public partial class frmQuestion_Student : UIForm
    {
        MyDbContext context = new();
        public frmQuestion_Student(StudentInfo student)
        {
            InitializeComponent();
            this.studentInfo = student;

		}

        StudentInfo studentInfo = new StudentInfo();
		Student_BLL student_BLL = new Student_BLL();

		//添加提问
		private void uiButton1_Click_1(object sender, EventArgs e)
        {
            string val = string.Empty;
            var result = this.ShowInputStringDialog(ref val, true, "请输入问题", true);
            if (result)
            {
                context.Question.Add(new Model.Question
                {
                    Q = val,
                    Status = "待回复",
                    StudentId = frmLogin.LoginStudentInfo.Id
                });
                context.SaveChanges();
                loadData();
            }
            studentInfo.AttendCount++;
            student_BLL.UpdateStudentInfoById(studentInfo);

		}
        //举手
        private void uiButton2_Click(object sender, EventArgs e)
        {
            context.Question.Add(new Model.Question
            {
                Q = "学生举手",
                Status = "待确认",
                StudentId = frmLogin.LoginStudentInfo.Id
            });
            context.SaveChanges();
            loadData();
        }

        private void frmQuestion_Student_Load(object sender, EventArgs e)
        {
            loadData();
        }

        void loadData()
        {
            context = new MyDbContext();
            dgvQuestion.DataSource = null;
            dgvQuestion.AutoGenerateColumns = false;
            dgvQuestion.DataSource = context.Question.Where(t => t.StudentId == frmLogin.LoginStudentInfo.Id).OrderByDescending(t => t.Id).ToList();
            dgvQuestion.Refresh();
        }
        private void dgvQuestion_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return; // 避免点击表头引发错误
            // 获取双击的行
            DataGridViewRow row = dgvQuestion.Rows[e.RowIndex];
            // 获取行数据
            var rowData = (Question)row.DataBoundItem;
            if (rowData.Status != "待确认")
            {
                frmQuestionDetail_Student s = new(rowData);
                s.ShowDialog(this);
                loadData();
            }
        }

        private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(new MethodInvoker(loadData));
            }
            else
            {
                loadData();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!backgroundWorker1.IsBusy)
                backgroundWorker1.RunWorkerAsync();
        }
    }
}
